/*
 * Copyright (c) 2023 Hong Jiahua
 * https://gitee.com/arrco/easypack
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __EASYPACK_H
#define __EASYPACK_H

/************************************************************************/
/*                                                                      */
/************************************************************************/
enum {
    EASYPACK_SUCCESS    =  0,
    EASYPACK_FAILURE    = -1,
};

enum {
    EASYPACK_FILE   = 0,
    EASYPACK_DIR    = 1,
};

typedef struct {
    void *fp;
    unsigned int pos;
    unsigned int remaining_data;
    unsigned int last_header;
} easypack_t;

#define EASYPACK_NAME_LEN   (24)
#define EASYPACK_HEADER_LEN (32)
#define EASYPACK_PAD_LEN    (EASYPACK_HEADER_LEN - EASYPACK_NAME_LEN - 4 - 1)

typedef struct {
    char name[EASYPACK_NAME_LEN];
    unsigned int size;
    char type;
} easypack_header_t;
/************************************************************************/
/*                                                                      */
/************************************************************************/
/*打开包文件*/
int easypack_open(easypack_t *obj, const char *filename, const char *mode);
/*关闭包文件*/
int easypack_close(easypack_t *obj);
/*读取文件的头信息*/
int easypack_read_header(easypack_t *obj, easypack_header_t *header);
/*读取文件数据*/
int easypack_read_data(easypack_t *obj, void *data, unsigned int size);
/*定位到起始位置*/
int easypack_rewind(easypack_t *obj);
/*定位到下一个文件头位置*/
int easypack_next(easypack_t *obj);
/*查找文件*/
int easypack_find(easypack_t *obj, const char *filename, easypack_header_t *header);
/*写入文件的头信息*/
int easypack_write_file_header(easypack_t *obj, const char *filename, unsigned int size);
/*写入文件夹的头信息*/
int easypack_write_dir_header(easypack_t *obj, const char *dirname);
/*写入文件数据*/
int easypack_write_data(easypack_t *obj, const void *data, unsigned int size);

#endif